package com.tonghl.design;

/**
 * Created by tonghl
 */
public class SingletonDemo {
    private  static volatile SingletonDemo  instance=null;

    private SingletonDemo(){
        System.out.println(Thread.currentThread().getName()+"\t 我是构造方法");
    }

    //DCL(Double Check Lock 双端检锁机制)
    public  static  SingletonDemo getInstance()
    {
        if (instance == null)//加锁前判断
        {

            synchronized (SingletonDemo.class)
            {
                if (instance == null)//加锁后判断
                {
                    instance=new SingletonDemo();
                }
            }

        }
        return  instance;
    }
    public static void main(String[] args) {


        for (int i = 1; i <=10 ; i++)
        {
            new Thread(()->
            {
                for (int j = 1; j <=1000; j++)
                {
                   SingletonDemo.getInstance();

                }
            },String.valueOf(i)).start();
        }
    }
}
